package com.jiantong.work.impl.service.analysis;

import com.google.common.base.Throwables;
import com.jiantong.work.dto.analysis.ScaleBreedFactoryArchivesFormDto;
import com.jiantong.work.impl.dao.*;
import com.jiantong.work.service.analysis.ScaleBreedFactoryArchivesFormReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by mz on 3/8/17.
 *
 */
@Slf4j
@Service
@RpcProvider
public class ScaleBreedFactoryArchivesFormReadServiceImpl implements ScaleBreedFactoryArchivesFormReadService{

    private final ProductionReportDao productionReportDao;

    private final FeedReportDao feedReportDao;

    private final VeterinaryDrugsReportDao veterinaryDrugsReportDao;

    private final ImmunizationSubRecordDao immunizationSubRecordDao;

    private final ImmunizationRecordDao immunizationRecordDao;

    private final MaterialPurchaseRecordDao materialPurchaseRecordDao;

    private final DrugPurchaseRecordDao drugPurchaseRecordDao;

    private final VaccinePurchaseRecordDao vaccinePurchaseRecordDao;

    private final SeedPurchaseRecordDao seedPurchaseRecordDao;

    private final OtherPurchaseRecordDao otherPurchaseRecordDao;

    @Autowired
    public ScaleBreedFactoryArchivesFormReadServiceImpl(ProductionReportDao productionReportDao,
                                                        FeedReportDao feedReportDao,
                                                        VeterinaryDrugsReportDao veterinaryDrugsReportDao,
                                                        ImmunizationSubRecordDao immunizationSubRecordDao,
                                                        ImmunizationRecordDao immunizationRecordDao,
                                                        MaterialPurchaseRecordDao materialPurchaseRecordDao,
                                                        DrugPurchaseRecordDao drugPurchaseRecordDao,
                                                        VaccinePurchaseRecordDao vaccinePurchaseRecordDao,
                                                        SeedPurchaseRecordDao seedPurchaseRecordDao,
                                                        OtherPurchaseRecordDao otherPurchaseRecordDao) {
        this.productionReportDao = productionReportDao;
        this.feedReportDao = feedReportDao;
        this.veterinaryDrugsReportDao = veterinaryDrugsReportDao;
        this.immunizationSubRecordDao = immunizationSubRecordDao;
        this.immunizationRecordDao = immunizationRecordDao;
        this.materialPurchaseRecordDao = materialPurchaseRecordDao;
        this.drugPurchaseRecordDao = drugPurchaseRecordDao;
        this.vaccinePurchaseRecordDao = vaccinePurchaseRecordDao;
        this.seedPurchaseRecordDao = seedPurchaseRecordDao;
        this.otherPurchaseRecordDao = otherPurchaseRecordDao;
    }

    /**
     * 分析每个养殖场的养殖档案数据
     *
     * @param criteria 参数
     * @return ScaleBreedFactoryArchivesFormDto
     */
    @Override
    public Response<ScaleBreedFactoryArchivesFormDto> analysisArchives(Map<String , Object> criteria) {
        try{
            //查询生产记录的出生、调入、调出、死淘的合计
            Map<String ,Object> amountOfProduction = productionReportDao.countSumBIOD(criteria);
            //查询饲料记录的用量
            Float feedSum = feedReportDao.countSumAmountFeed(criteria);
            //查询兽药记录的次数
            Long drugRecordCounts = veterinaryDrugsReportDao.countVDRTimes(criteria);
            //查询免疫记录次数
            Long immunizationSubRecordCounts = immunizationSubRecordDao.countISRTimes(criteria);
            //查询原料购置的map
            Map<String , Object> amountOfMaterialPurchase = materialPurchaseRecordDao.countAmountOfMaterial(criteria);
            //查询药物购置的map
            Map<String , Object> amountOfDrugPurchase = drugPurchaseRecordDao.countAmountOfDrug(criteria);
            //查询疫苗购置的map
            Map<String , Object> amountOfVaccinePurchase = vaccinePurchaseRecordDao.countAmountOfVaccine(criteria);
            //查询引种记录的map
            Map<String , Object> amountOfSeedPurchase = seedPurchaseRecordDao.countAmountOfSeed(criteria);
            //查询其他购买的map
            Map<String , Object> amountOfOtherPurchase = otherPurchaseRecordDao.countAmountOfOther(criteria);
            //计算采购的总金额
            Double amountOfPurchase = 0d;
            //原料购置
            if(amountOfMaterialPurchase == null || amountOfMaterialPurchase.isEmpty()) amountOfPurchase = amountOfPurchase + 0d;
            else amountOfPurchase = amountOfPurchase + (Double) amountOfMaterialPurchase.get("totalPrice");
            //药物购置
            if(amountOfDrugPurchase == null || amountOfDrugPurchase.isEmpty()) amountOfPurchase = amountOfPurchase + 0d;
            else amountOfPurchase = amountOfPurchase + (Double) amountOfDrugPurchase.get("totalPrice");
            //疫苗购置
            if(amountOfVaccinePurchase == null || amountOfVaccinePurchase .isEmpty()) amountOfPurchase = amountOfPurchase + 0d;
            else amountOfPurchase = amountOfPurchase + (Double) amountOfVaccinePurchase.get("totalPrice");
            //引种记录
            if(amountOfSeedPurchase == null || amountOfSeedPurchase .isEmpty()) amountOfPurchase = amountOfPurchase + 0d;
            else amountOfPurchase = amountOfPurchase + (Double) amountOfSeedPurchase.get("totalPrice");
            //其他购买
            if(amountOfOtherPurchase == null || amountOfOtherPurchase .isEmpty()) amountOfPurchase = amountOfPurchase + 0d;
            else amountOfPurchase = amountOfPurchase + (Double) amountOfOtherPurchase.get("totalPrice");

            //构建养殖场的数据分析对象
            ScaleBreedFactoryArchivesFormDto scaleBreedFactoryArchivesFormDto = new ScaleBreedFactoryArchivesFormDto();

            scaleBreedFactoryArchivesFormDto.setAmountOfProduction(amountOfProduction);
            scaleBreedFactoryArchivesFormDto.setFeedSum(feedSum);
            scaleBreedFactoryArchivesFormDto.setDrugRecordCounts(drugRecordCounts);
            scaleBreedFactoryArchivesFormDto.setImmunizationSubRecordCounts(immunizationSubRecordCounts);
            scaleBreedFactoryArchivesFormDto.setAmountOfMaterialPurchase(amountOfMaterialPurchase);
            scaleBreedFactoryArchivesFormDto.setAmountOfDrugPurchase(amountOfDrugPurchase);
            scaleBreedFactoryArchivesFormDto.setAmountOfVaccinePurchase(amountOfVaccinePurchase);
            scaleBreedFactoryArchivesFormDto.setAmountOfSeedPurchase(amountOfSeedPurchase);
            scaleBreedFactoryArchivesFormDto.setAmountOfOtherPurchase(amountOfOtherPurchase);
            scaleBreedFactoryArchivesFormDto.setAmountOfPurchase(amountOfPurchase);

            return Response.ok(scaleBreedFactoryArchivesFormDto);
        }catch (Exception e){
            log.error("failed to find this Archives case = {}", Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find this Archives");
        }
    }
}
